package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity findLikePages(
            @PathVariable("type") String type,
            @RequestParam Integer page,
            @RequestParam(required = false) Integer pagesize,
            @RequestParam(required = false) String nickname
    ){
        PageResult pr = userInfoService.findLikePages(page,pagesize,nickname,type);
        return ResponseEntity.ok(pr);
    }


    /**
     * 喜欢粉丝
     * @param fansId
     * @return
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity saveLikeFans(@PathVariable("uid") Integer fansId){
        userInfoService.saveLikeFans(fansId);
        return ResponseEntity.ok(null);
    }

    /**
     * 喜欢 - 取消
     * @param disId
     * @return
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity disLikeFans(@PathVariable("uid") Integer disId){
        //1、调用API，更新喜欢数据(在MongoDB中)
        userInfoService.disLikeFans(disId);

        //2、如果是friend,断绝朋友关系,但是环信有问题,就先不做了
        return ResponseEntity.ok(null);
    }



    /**
     * 更新头像
     */
    @PostMapping("/header")
    public ResponseEntity updateHead(MultipartFile headPhoto) throws IOException {
        Long id = UserHolder.getUserId();
        userInfoService.updateHead(headPhoto,id);
        return ResponseEntity.ok(null);
    }
    /**
     * 修改手机号 1.发送短信验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity updatePhone(){
        String mobile = UserHolder.getMobile();
        userInfoService.sendMesg(mobile);
        return ResponseEntity.ok(null);
    }
    /**
     * 2.校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        String verificationCode = (String) map.get("verificationCode");
        String mobile = UserHolder.getMobile();
        Map map1 = userInfoService.checkVerificationCode(mobile,verificationCode);
        return ResponseEntity.ok(map1);
    }
    /**
     * 保存
     */
    @PostMapping("/phone")
    public ResponseEntity savePhone(@RequestBody Map map){
        Long userId = UserHolder.getUserId();
        String phone = (String) map.get("phone");
        userInfoService.savePhone(userId,phone);
        return ResponseEntity.ok(null);
    }
    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Map map = userInfoService.counts();
        return ResponseEntity.ok(map);
    }
}
